package view;

import com.alee.extended.painter.BorderPainter;
import com.alee.laf.label.WebLabel;
import com.alee.laf.panel.WebPanel;
import controller.App;
import model.UserModel;
import util.FontLoader;
import util.ImageLoader;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class UserView extends WebPanel {
    private static final long serialVersionUID = -1180602255891382960L;
    
    public UserModel model;
    private String query = "";

    /**
     * Create the user view with a model.
     * @param model Model to create user view from
     */
    public UserView(final UserModel model) {
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        this.setUndecorated(false);
        this.model = model;
        this.add(this.createUser(null));
        this.add(Box.createVerticalStrut(10));
        this.setupView();
        this.createLabels(null);
    }

    /**
     * Create a user view with a model and a query to highlight
     * @param model Model to create user view from
     * @param query Query to highlight text based on
     */
    public UserView(final UserModel model, String query) {
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        this.setUndecorated(false);
        this.model = model;
        this.add(this.createUser(query));
        this.add(Box.createVerticalStrut(10));
        this.setupView();
        this.createLabels(query);
    }

    /**
     * Setup base styles
     */
    private void setupView() {
        final BorderPainter bp2 = new BorderPainter ();
        bp2.setRound(2);
        bp2.setWidth(1);
        bp2.setColor(Color.lightGray);
        this.setPainter(bp2);
        this.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.lightGray));
        this.setMargin(10);
        this.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent mouseEvent) {
                App.app.showProfile(model.userId);
            }

            @Override
            public void mousePressed(MouseEvent mouseEvent) {
            }

            @Override
            public void mouseReleased(MouseEvent mouseEvent) {
            }

            @Override
            public void mouseEntered(MouseEvent mouseEvent) {
                bp2.setColor(new Color(80, 58, 150));
                repaint();
            }

            @Override
            public void mouseExited(MouseEvent mouseEvent) {
                bp2.setColor(Color.lightGray);
                repaint();
            }
        });
    }

    /**
     * Creates an html string where instances of the query found in the text are highlighted
     * @param text Text to search/highlight
     * @param query Query to use for highlighting
     * @return HTML string
     */
    private String wrapQuery(String text, String query) {
        int start = text.toLowerCase().indexOf(query.toLowerCase());
        if (start > -1) {
            String e = text.substring(start + query.length());
            e = "</font></b>" + e;
            String f = text.substring(0, start);
            f += "<b><font color='purple'>";
            text = f + text.substring(start, start + query.length()) + e;
        }
        return text;
    }

    /**
     * Creates the various labels for the user view.
     * @param query If not null, labels that match the query have the query highlighted.
     */
    private void createLabels(String query) {
        String major = model.major;
        String location = model.city + ", " + model.state;

        if (query != null) {
            major = wrapQuery(major, query);
            location = wrapQuery(location, query);
        }

        this.add(this.createLabel("<html><b>Major:</b> " + major + "</html>"));

        if (!model.city.isEmpty() && !model.state.isEmpty()) {
            this.add(this.createLabel("<html><b>Location:</b> " + location + "</html>"));
        } else {
            this.add(this.createLabel("<html><b>Location:</b> Unknown </html>"));
        }
    }


    /**
     * Create user name label.
     * @param query Query to highlight
     * @return Label component for user.
     */
    private WebLabel createUser(String query) {
        String label;
        if (query != null) {
            label = "<html>" + wrapQuery(model.name, query) + "</html>";
        } else {
            label = model.name;
        }
        WebLabel authorLabel = new WebLabel(label, ImageLoader.getImage("kanye.png"));
        Font osBold = FontLoader.getFont(FontLoader.OPENSANS_BOLD);
        authorLabel.setFont(osBold.deriveFont(14f));
        authorLabel.setForeground(new Color(243, 117, 102));
        authorLabel.setMargin(2, 8, 0, 8);
        authorLabel.setIconTextGap(20);
        return authorLabel;
    }
    
    /**
     * Create a label
     * @param label Label text 
     * @return Label component
     */
    private WebLabel createLabel(String label) {
        Font osRegular = FontLoader.getFont(FontLoader.OPENSANS_REGULAR);
        WebLabel timeLabel = new WebLabel(label);
        timeLabel.setFont(osRegular.deriveFont(12f));
        timeLabel.setForeground(new Color(100, 100, 100));
        return timeLabel;
    }
}
